<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Math - The Commons Math User Guide - Fractions</title>
    <style type="text/css" media="all">
      @import url("../css/maven-base.css");
      @import url("../css/maven-theme.css");
      @import url("../css/site.css");
    </style>
    <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                    <span id="bannerLeft">
    
            Commons Math User Guide
    
            </span>
                    <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
    
              <div class="xright">      
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
    
                   <h5>User Guide</h5>
            <ul>
              
    <li class="none">
                    <a href="../userguide/index.html">Contents</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/overview.html">Overview</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/stat.html">Statistics</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/random.html">Data Generation</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/linear.html">Linear Algebra</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/analysis.html">Numerical Analysis</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/special.html">Special Functions</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/utilities.html">Utilities</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/complex.html">Complex Numbers</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/distribution.html">Distributions</a>
          </li>
              
    <li class="none">
              <strong>Fractions</strong>
        </li>
              
    <li class="none">
                    <a href="../userguide/transform.html">Transform Methods</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/geometry.html">3D Geometry</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/optimization.html">Optimization</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/ode.html">Ordinary Differential Equations</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/genetics.html">Genetic Algorithms</a>
          </li>
          </ul>
                                           <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
    
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2><a name="a9_Fractions"></a>
9 Fractions</h2>
<div class="section"><h3><a name="a9.1_Overview"></a>
9.1 Overview</h3>
<p>
          The fraction packages provides a fraction number type as well as
          fraction number formatting.
        </p>
</div>
<div class="section"><h3><a name="a9.2_Fraction_Numbers"></a>
9.2 Fraction Numbers</h3>
<p><a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
          org.apache.commons.math.fraction.Fraction</a>
 and
          <a href="../apidocs/org/apache/commons/math/fraction/BigFraction.html">
          org.apache.commons.math.fraction.BigFraction</a>
 provide fraction number
          type that forms the basis for the fraction functionality found in 
          commons-math. The former one can be used for fractions whose numerators
          and denominators are small enough to fit in an int (taking care of intermediate
          values) while the second class should be used when there is a risk the numerator
          and denominator grow very large.
        </p>
<p>
          A fraction number, can be built from two integer arguments representing numerator
          and denominator or from a double which will be approximated:
          <div class="source"><pre>Fraction f = new Fraction(1, 3); // 1 / 3
Fraction g = new Fraction(0.25); // 1 / 4</pre>
</div>
</p>
<p>
          Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.
        </p>
<p>
          The <code>Fraction</code> class provides many unary and binary
          fraction operations.  These operations provide the means to add,
          subtract, multiple and, divide fractions along with other functions similar to the real number functions found in
          <code>java.math.BigDecimal</code>:
          <div class="source"><pre>Fraction lhs = new Fraction(1, 3);
Fraction rhs = new Fraction(2, 5);

Fraction answer = lhs.add(rhs);     // add two fractions
        answer = lhs.subtract(rhs); // subtract two fractions
        answer = lhs.abs();         // absolute value
        answer = lhs.reciprocal();  // reciprocal of lhs</pre>
</div>
</p>
<p>
          Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.
        </p>
</div>
<div class="section"><h3><a name="a9.3_Fraction_Formatting_and_Parsing"></a>
9.3 Fraction Formatting and Parsing</h3>
<p><code>Fraction</code> instances can be converted to and from strings
          using the<a href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
          org.apache.commons.math.fraction.FractionFormat</a>
 class.
          <code>FractionFormat</code> is a <code>java.text.Format</code>
          extension and, as such, is used like other formatting objects (e.g.
          <code>java.text.SimpleDateFormat</code>):
          <div class="source"><pre>FractionFormat format = new FractionFormat(); // default format
Fraction f = new Fraction(2, 4);
String s = format.format(f); // s contains &quot;1 / 2&quot;, note the reduced fraction</pre>
</div>
</p>
<p>
          To customize the formatting output, one or two
          <code>java.text.NumberFormat</code> instances can be used to construct
          a <code>FractionFormat</code>.  These number formats control the
          formatting of the numerator and denominator of the fraction:
          <div class="source"><pre>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
// create fraction format with custom number format
// when one number format is used, both numerator and
// denominator are formatted the same
FractionFormat format = new FractionFormat(nf);
Fraction f = new Fraction(2000, 3333);
String s = format.format(c); // s contains &quot;2.000 / 3.333&quot;

NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
// create fraction format with custom number formats
format = new FractionFormat(nf, nf2);
s = format.format(f); // s contains &quot;2.000 / 3,333&quot;</pre>
</div>
</p>
<p>
          Formatting's inverse operation, parsing, can also be performed by
          <code>FractionFormat</code>.  To parse a fraction from a string,
          simply call the <code>parse</code> method:
          <div class="source"><pre>FractionFormat ff = new FractionFormat();
Fraction f = ff.parse(&quot;-10 / 21&quot;);</pre>
</div>
</p>
</div>
</div>

      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2003-2009
    
          
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
